#!/bin/bash

# 副将系统数据库初始化脚本
# 使用远程MySQL数据库

# 数据库配置
DB_HOST="43.143.253.188"
DB_PORT="3306"
DB_USER="nextjs"
DB_PASS="yEAiRE2jcxTyRBy5"
DB_NAME="nextjs"

# SQL文件路径
SQL_FILE="../database/init-deputy-general-tables.sql"

echo "🚀 开始初始化副将系统数据库..."
echo "数据库: ${DB_HOST}:${DB_PORT}/${DB_NAME}"
echo "用户: ${DB_USER}"
echo ""

# 检查SQL文件是否存在
if [ ! -f "$SQL_FILE" ]; then
    echo "❌ SQL文件不存在: $SQL_FILE"
    exit 1
fi

# 执行SQL脚本
echo "📝 正在执行SQL脚本..."
mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" < "$SQL_FILE"

if [ $? -eq 0 ]; then
    echo "✅ 副将系统表初始化成功！"
    echo ""
    echo "📊 验证表结构..."
    
    # 验证表是否创建成功
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASS" "$DB_NAME" -e "
    SELECT 'skill_base' as table_name, COUNT(*) as record_count FROM skill_base
    UNION ALL
    SELECT 'deputy_general' as table_name, COUNT(*) as record_count FROM deputy_general
    UNION ALL
    SELECT 'player_deputy' as table_name, COUNT(*) as record_count FROM player_deputy
    UNION ALL
    SELECT 'deputy_general_skill_learned' as table_name, COUNT(*) as record_count FROM deputy_general_skill_learned
    UNION ALL
    SELECT 'deputy_general_equipment' as table_name, COUNT(*) as record_count FROM deputy_general_equipment
    UNION ALL
    SELECT 'deputy_general_stone' as table_name, COUNT(*) as record_count FROM deputy_general_stone
    UNION ALL
    SELECT 'player_deputy_warehouse' as table_name, COUNT(*) as record_count FROM player_deputy_warehouse
    UNION ALL
    SELECT 'deputy_general_level_log' as table_name, COUNT(*) as record_count FROM deputy_general_level_log
    UNION ALL
    SELECT 'deputy_general_star_log' as table_name, COUNT(*) as record_count FROM deputy_general_star_log
    UNION ALL
    SELECT 'deputy_general_trans_log' as table_name, COUNT(*) as record_count FROM deputy_general_trans_log;
    "
    
    echo ""
    echo "🎉 副将系统数据库初始化完成！"
    echo "现在可以启动NestJS应用程序了。"
else
    echo "❌ 数据库初始化失败！"
    exit 1
fi
